package com.dsx.myblog.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dsx
 */
@Controller
@RequestMapping("/testRedis")
public class RedisStringController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @RequestMapping(value = "/pipeline", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> testPipeline() {
        Long start = System.currentTimeMillis();

        List list = redisTemplate.executePipelined(new SessionCallback() {
                @Override
                public Object execute(RedisOperations redisOperations) throws DataAccessException {
                    String value = (String) redisOperations.opsForValue().get("int");
                    System.out.println("命令只是进入队列，所以值为空【" + value + "】");
                    return null;
                }
            }
        );
        Long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start) + "毫秒。");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", true);
        return map;
    }

    @RequestMapping(value = "/stringAndHash", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> testStringAndHash() {
        redisTemplate.opsForValue().set("key1", "value1");
        // 注意这里使用了JDK的序列化器，所以Redis保存的时候不是整数，不能运算
        redisTemplate.opsForValue().set("int_key", "1");
        stringRedisTemplate.opsForValue().set("int", "1");
        // 使用运算
        stringRedisTemplate.opsForValue().increment("int", 1);
//        // 获取底层Jedis连接
//        Jedis jedis = (Jedis) stringRedisTemplate.getConnectionFactory().getConnection().getNativeConnection();
//        // 减一操作,这个命令RedisTemplate不支持，所以笔者先获取底层的连接再操作
//        jedis.decr("int");
        Map<String, String> hash = new HashMap<String, String>();
        hash.put("field1", "value1");
        hash.put("field2", "value2");
        // 存入一个散列数据类型
        stringRedisTemplate.opsForHash().putAll("hash", hash);
        // 新增一个字段
        stringRedisTemplate.opsForHash().put("hash", "field3", "value3");
        // 绑定散列操作的key，这样可以连续对同一个散列数据类型进行操作
        BoundHashOperations hashOps = stringRedisTemplate.boundHashOps("hash");
        // 删除两个个字段
        hashOps.delete("field1", "field2");
        // 新增一个字段
        hashOps.put("filed4", "value5");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", true);
        return map;
    }


    @ResponseBody
    @RequestMapping(value = "/test/{key}", method = RequestMethod.GET)
    public String get(@PathVariable String key) {
        String str = null;
        try {
            System.out.println("key---> " + key);
            str = stringRedisTemplate.opsForValue().get(key);
//            redisClient.opsFor
            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return str;
    }


}
